
/**    
 * <p>Copyright： 东软烟草产品研发团队</p>   
 */

package com.jkl.service.menu;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import com.jkl.entity.menu.BsPersonDTO;
import com.jkl.entity.menu.ButtonVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jkl.dao.menu.BsMenuMapper;
import com.jkl.entity.menu.BsMenu;
import com.jkl.service.menu.inter.BsMenuServiceInterface;
import com.jkl.untils.MenuUntil;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 123service
 * <p>
 * <p>
 * 123service
 * <p>
 * <p>
 * 变更说明：lin.pf 2018年1月17日 123service
 * <p>
 * Date:2018年10月01日
 * 
 * @author lin.pf
 * @version 1.0
 */
@Service
@Slf4j
public class BsMenuService implements BsMenuServiceInterface {

	@Autowired
	private BsMenuMapper bsMenuMapper;

	@Override
	public List<BsMenu> getAllMenu() {
		return MenuUntil.assembleMenu(bsMenuMapper.getAll());
	}

	@Override
	public List<Integer> getMenuIdByroleId(int roleId) {
		return bsMenuMapper.getMenuIdByroleId(roleId);
	}

	@Override
	public List<BsMenu> getAllBsMenu() {
		return bsMenuMapper.getAll();
	}

	@Override
	public List<BsMenu> getBsMenu(List<Integer> menuId) {
		return 	bsMenuMapper.getSomeMenu(menuId);

	}

	@Override
	public Integer selectByRowId(Integer integer) {
		return bsMenuMapper.selectByRowId(integer);
	}

	@Override
	public List<BsMenu> getAllLevel1Menu() {
		return bsMenuMapper.getAllLevel1Menu();
	}

	@Override
	public List<BsMenu> getAllLevel2Menu() {
		return bsMenuMapper.getAllLevel2Menu();
	}

	@Override
	public List<BsPersonDTO> getAllMessage(Integer rowId) {
		//根据传来的菜单id查到所有信息
		List<BsPersonDTO> allMessage = bsMenuMapper.getAllMessage(rowId);
		//遍历这个List中的所有角色id,将角色id去重
		List<Integer> roleIds = new ArrayList<>();
		Set<Integer> set = new HashSet<>();
		allMessage.forEach(i->roleIds.add(i.getRoleId()));
		set.addAll(roleIds);
		roleIds.clear();
		roleIds.addAll(set);
		//根据去重的角色id查出所有的按钮
		for (Integer roleId : roleIds) {
			//根据菜单id和角色id查询到所有的按钮名
			String[] allButtonName = bsMenuMapper.getAllButtonName(roleId, rowId);
			//根据角色ID将按钮放入一个string中，用逗号隔开
			String buttonName = StringUtils.join(allButtonName, ",");
			for (int i = 0; i < allMessage.size(); i++) {
				if (roleId.equals(allMessage.get(i).getRoleId())){
					//将字符串放入到allMessage中
					allMessage.get(i).setButtonName(buttonName);
				}
			}

		}


		return allMessage;
	}
}